//
//  QLMainViewController.swift
//  WeiBo
//
//  Created by 黄跃奇 on 16/9/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class QLMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        
        //使用kvc方式给oc中的类设置只读属性的值
        let qlTabBar = QLTabBar()
        setValue(qlTabBar, forKey: "tabBar")
        
        //MARK: 5. (闭包)传入闭包执行
        //在此使用self会产生循环引用, 解决办法使用[weak self]
        qlTabBar.composeButtonClosure = { [weak self] in
            print("我是闭包调用过来的")
        }
        
        //MARK: 4. (代理)设置代理对象
        qlTabBar.qlDelegate = self
        
        //添加子视图控制器
        addChildViewController(childController: QLHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: QLMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: QLDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: QLProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    
    //添加子视图控制器的方法重载
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        //设置tabBarItem的图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //设置tabBarItem选中状态的图片及渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        
        //设置tabBarItem及navigationItem的title
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        //上面两句可以合并
        childController.title = title
        
        //设置选中的文字的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: UIControlState.selected)
        
        //设置文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
        //创建导航视图控制器
        let nav = QLNavigationViewController(rootViewController: childController)
        
        //添加子视图控制器
        addChildViewController(nav)
    }
}

//MARK: 5. (代理)使用extension可以实现代理方法
extension QLMainViewController: QLTabBarDelegate {
    
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
    
}







